<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <!-- table表格内容 -->
      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式">
            <template v-slot="{row}">
              {{ formatEmployees(row.formOfEmployment) }}
            </template>
          </el-table-column>

          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template v-slot="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page="q.page"
            :page-sizes="[2, 5, 10, 15]"
            :page-size="q.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>

      <!-- 新增员工弹层 -->
      <el-dialog
        title="新增员工"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <addor-edit v-if="showDialog" @close="showDialog=false" @updateEmployee="updateEmployee" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getEmployeesList, delEmployee } from '@/api/employees'
import pageTools from '@/components/pageTools'
import employeesStyle from '@/constant/employees.js'
import addorEdit from './empDialog'

export default {
  components: {
    pageTools,
    addorEdit
  },
  data() {
    return {
      showDialog: false,
      list: [],
      q: {
        page: 1,
        size: 10
      },
      total: 0
    }
  },

  created() {
    this.loadEmployeesList()
  },
  methods: {
    updateEmployee() {
      this.showDialog = false
      this.total++
      this.q.page = Math.ceil(this.total / this.q.size)
      this.loadEmployeesList()
    },
    handleCurrentChange(page) {
      this.q.page = page
      this.loadEmployeesList()
    },
    handleSizeChange(size) {
      this.q.size = size
      this.loadEmployeesList()
    },
    async loadEmployeesList() {
      try {
        const res = await getEmployeesList(this.q.page, this.q.size)
        // console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (err) {
        this.$message.error('加载员工数据失败')
      }
    },
    formatEmployees(num) {
      const obj = employeesStyle.hireType.find(item => item.id === num)
      if (obj) {
        return obj.value
      } else {
        return '未知'
      }
    },
    hDel(id) {
      this.$confirm('确认删除吗').then(async() => {
        try {
          const res = await delEmployee(id)
          this.$message.success(res.message)
          if (this.list.length === 1 && this.q.page > 1) {
            this.q.page--
          }
          this.loadEmployeesList()
        } catch (err) {
          console.log(err)
        }
      }).catch(() => {})
    }
  }
}
</script>
